import utils


def container_format(ip, name, data, t):
    value = str(round(float(data['value'][1]), 6))
    return name.rstrip("\n") + ":" + data['metric']['name'] + ":" + ip + ":" + t + ":" + value


def container_dif_format(ip, name, data, t):
    value = str(round(float(data['value'][1]), 6))
    return name.rstrip("\n") + ":" + "ct" + ":" + ip + ":" + t + ":" + value


def collect_container():
    msgs = []
    opts = utils.read_data("/collector_slave/config/opts/opts_container.txt")
    names = utils.read_data("/collector_slave/config/names/names_container.txt")
    for opt, name in zip(opts, names):
        opt = opt.rstrip("\n")
        r = utils.collect_data("localhost", opt)
        for data in r['data']['result']:
            msgs.append(container_format(utils.get_network_ip(), name, data, utils.get_ms_time()))

    opts = utils.read_data("/collector_slave/config/opts/opts_dif_format.txt")
    names = utils.read_data("/collector_slave/config/names/names_dif_format.txt")
    for opt, name in zip(opts, names):
        opt = opt.rstrip("\n")
        r = utils.collect_data("localhost", opt)
        for data in r['data']['result']:
            msgs.append(container_dif_format(utils.get_network_ip(), name, data, utils.get_ms_time()))
    return msgs
